package cn.lds.j368.dsbank.controller;

import cn.lds.j368.dsbank.domain.Employee;
import cn.lds.j368.dsbank.domain.Role;
import cn.lds.j368.dsbank.dto.RespDTO;
import cn.lds.j368.dsbank.exception.ServiceException;
import cn.lds.j368.dsbank.service.EmployeeService;
import cn.lds.j368.dsbank.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RoleService roleService;


    @GetMapping("/show_emps")
    public String showEmpAll(Model model) throws ServiceException {
        RespDTO<List<Employee>> listRespDTO = employeeService.selectEmpAll();
        model.addAttribute("results",listRespDTO);
        return "/emp/show_emp";
    }

    @GetMapping("/show_detail/{empId}")
    public String showEmpById(@PathVariable Integer empId,Model model) throws ServiceException {

        RespDTO<Employee> employeeRespDTO = employeeService.selectEmpByEmpId(empId);

        model.addAttribute("result",employeeRespDTO);

        return "/emp/show_emp_details";
    }

    @GetMapping("add_emp")
    public String addEmp(Model model, HttpSession session){

        List<Role> roles = roleService.selectRoleAll();

        model.addAttribute("roles",roles);

        return "/emp/add_emp";
    }

    @PostMapping("/do_add_emp")
    public String doAddEmp(Employee employee,Integer... roles) throws ServiceException {

        employee.setCreateEmpId(1);
        employeeService.addEmp(employee,roles);

        return "redirect:/emp/show_emps";
    }

    @GetMapping("modify_emp/{empId}")
    public String updateEmp(@PathVariable Integer empId,Model model,HttpSession session) throws ServiceException {

        //获取要修改的员工信息
        Employee emp = employeeService.selectEmpByEmpId(empId).getData();


        List<Role> roles = roleService.selectRoleAll();

        model.addAttribute("roles",roles);

        model.addAttribute("emp",emp);



        return "/emp/modify_emp";
    }

    @PostMapping("/do_modify_emp")
    public String doUpadteEmp(Employee employee, @AuthenticationPrincipal Employee loginEmployee) throws ServiceException {

        employee.setModifyEmpId(loginEmployee.getEmpId());

        employeeService.modifyEmp(employee);


        return "redirect:/emp/show_emps";

    }

    @GetMapping("del_emp/{empId}")
    public String delEmp(@PathVariable Integer empId){
        return "redirect:/emp/show_emps";
    }



    @GetMapping("login")
    public String login(){
        return "login";
    }
}
